﻿/**	\file		lxpref_test_main.cpp
 *	\date		(2014-11-23 17:44:26)/(2015-01-01 00:38:43)
 *-----------------------------------------------------------------------------
 *	\brief
 *	\version	1.0.0.1
 *	\author		Nick Shallery	(nicknide@gmail.com)
 *	\copyright	YOU HAVE ALL LEFT WITH THIS CODE WHILE YOU KEEP COMMENT IN EVERY COPY.
 *-----------------------------------------------------------------------------
**/

#include	<lolix/imp/lolix_plugin_enter.inl>
#include	<lolix/imp/lolix_plugin_auto_release_lib.inl>

#include	"../lxpref/lxpref.h"

#include	<lxtrial/icase.h>
#include	<lxtrial/case_pool.h>

namespace lxtrial{class icase;}

lolix::LOLIX_RETNV
__CreateLxPrefCase(lxtrial::icase** out_ptr, lolix::dynamic_allocator* alc, lolix::ref_factory* ref_fac, lolix::toy::time_info::factory_type* tinf_fac, lolix::loli::thread_factory* td_fac, lxpref::lxpref_fac* pref_fac, lxpref::lxprofile* pref);
lxtrial::icase* g_icase;
lxtrial::case_pool* g_case_pool;

extern "C" lolix::LOLIX_RETNV	LOLIX_CALL lxpLoading(lolix::lolix_inst* /*lx_inst*/, lolix::lolix_regist* lx_reg)
{
	lolix::LOLIX_RETNV rtv;
	lolix::dynamic_allocator* alc;
	if ( LOLIX_CHECK_ERROR(rtv = lx_reg->query_itf(&alc)) )
		return rtv;
	atexit_invoke(*alc, release());
	lolix::ref_factory* ref_fac;
	if ( LOLIX_CHECK_ERROR(rtv = lx_reg->query_itf(&ref_fac)) )
		return rtv;
	atexit_invoke(*ref_fac, release());
	//TODO: add more init code here
	lxpref::lxprofile* prf;
	if ( LOLIX_CHECK_ERROR(rtv = lx_reg->query_itf(&prf)) )
		return rtv;
	atexit_release(*prf);

	lxtrial::case_pool* cp;
	if ( LOLIX_CHECK_ERROR(rtv = lx_reg->query_itf(&cp)) )
		return rtv;
	atexit_release(*cp);

	lolix::loli::thread_factory* td_fac;
	if ( LOLIX_CHECK_ERROR(rtv = lx_reg->query_itf(&td_fac)) )
		return rtv;
	atexit_release(*td_fac);

	lolix::toy::time_info::factory_type* tinf_fac;
	if ( LOLIX_CHECK_ERROR(rtv = lx_reg->query_itf(&tinf_fac)) )
		return rtv;
	atexit_release(*tinf_fac);

	lxpref::lxpref_fac* pref_fac;
	if ( LOLIX_CHECK_ERROR(rtv = lx_reg->query_itf(&pref_fac)) )
		return rtv;
	atexit_release(*pref_fac);

	lxtrial::icase* ic;
	if ( LOLIX_CHECK_ERROR(rtv = __CreateLxPrefCase(&ic, alc, ref_fac, tinf_fac, td_fac, pref_fac, prf)) )
		return rtv;
	atexit_release(*ic);
	cp->add_case(ic);
	(g_icase = ic)->inc_ref();
	(g_case_pool = cp)->inc_ref();
	//lolix::LOLIX_ITF_ID ic_id;
	//ic->itf_id(&ic_id);
	//if ( LOLIX_CHECK_ERROR(rtv = lx_reg->reg_loli(&ic_id, ic)) )
	//	return rtv;
	return rtv;
}


extern "C" lolix::LOLIX_RETNV	LOLIX_CALL lxpFreeing(lolix::lolix_inst* lx_inst)
{
	if ( g_icase->release() > 0 )
	{
		//lolix::LOLIX_RETNV rtv;
		g_case_pool->rmv_case(g_icase);
	}
	g_case_pool->release();
	return lolix::imp::plugin_auto_release_lib_query(lx_inst);
}

#if			0
static lolix::LOLIX_RETNV process_enter(void)
{
}

static void process_leave(void)
{
}

static void thread_enter(void)
{
}

static void thread_leave(void)
{
}

#else	//	0
#define	process_enter	0
#define	process_leave	0
#define	thread_enter	0
#define	thread_leave	0
#endif	//	0

#define	LXPREF_TEST_LIB_ID		0x95218af5, 0xfc2a4bb5, 0xa57366a1, 0x87552995
LOLIX_DEF__PLUGIN_ENTER(LXPREF_TEST_LIB_ID, "lxpref_test"
	, process_enter, process_leave, thread_enter, thread_leave);
